<?php

class Services extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('services_model');
        $this->form_validation->set_error_delimiters('', ' ');
        $this->session->set_userdata('user_id', '1');
    }
    
    function consume_webservice(){
        $called_service = $this->input->post('service_name');
        if ($called_service){
            if (call_user_func($called_service) === FALSE)
                $this->_show_error(SERVICE_NOT_FOUND_ERROR);
        }else{
            $this->_show_error(SERVICE_NOT_FOUND_ERROR);
        }
        
    }
    
    function dashboard(){
        if ($this->_is_user_logged()) 
            $this->_show_ok_response();
        else
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
    }
    
    /*
     * author: Lidier
     * purpose: Create a new user according to the information provided
     * parameters:
     *
     */
    function user_reg(){
        $user = array(
            'username' => $this->input->post('username'),
            'password' => sha1($this->input->post('password')),
            //'password_confirmation' => sha1($this->input->post('password_confirmation')),
            'email' => $this->input->post('email'),
            'firstname' => $this->input->post('firstname'),
            'secondname' => $this->input->post('secondname'),
            'lastname1' => $this->input->post('lastname1'),
            'lastname2' => $this->input->post('lastname2'),
            'id_number' => $this->input->post('id_number'),
            'creation_date' => date(DATE_FORMAT),
            'state' => USER_ACTIVE
        );
        
        if ($this->form_validation->run()) {
            unset($user['password_confirmation']);
            $user['id'] = $this->services_model->user_reg($user);
            if (isset($user['id']) && $user['id'] !== FALSE)
            {
                unset($user['password']);
                $this->_show_ok_response($user);
            }
            else 
                $this->_show_error(REGISTRATION_ERROR);            
        }
        else{
            $this->_show_form_validation_errors($user);
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: Authenticate an user an create the session data for him
     * parameters:
     *
     */
    function login(){
        $this->session->sess_destroy();
        $data = array(
            'username' => $this->input->post('username'),
            'password' => sha1($this->input->post('password')),
            'state' => USER_ACTIVE
        );
        if ($this->form_validation->run()) {
            $data['user_id'] = $this->services_model->login($data);
            if (isset($data['user_id']) && $data['user_id'] !== FALSE){
                unset($data['password']);
                $this->_create_user_session($data);
                $this->_show_ok_response();
            }
            else 
                $this->_show_error(LOGIN_ERROR);           
        }
        else {
            $this->_show_form_validation_errors($data);
        }
    }
    
    
   
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */

    function get_products_from_store() {
        $data = array(
            'store_id' => $this->input->post('store_id')
        );

        if ($this->form_validation->run()) {
            $response = $this->services_model->get_products_from_store($data);
            if ($response === FALSE)
                $this->_show_error(UNKNOWN_ERROR);
            else
                $this->_show_ok_response($response);
        }
        else {
            $this->_show_form_validation_errors($data);
        }
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function get_all_stores(){
        $response = $this->services_model->get_all_stores();
        if ($response === FALSE)
            $this->_show_error(UNKNOWN_ERROR);
        else
            $this->_show_ok_response($response);
        
    }
    
    //*****************************************************************************
    // LOGIN REQUIRED FOR ALL THESE!!
     /*
     * author: Lidier
     * purpose: Destroy all the session data of the currently logged in user
     * parameters:
     *
     */
    function logout(){
        $this->session->sess_destroy();
        $this->_show_ok_response();
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function store_reg(){
        if ($this->_is_user_logged()) 
        {
            $data = array(
                'owner_id' => $this->session->userdata('user_id'),
                'name' => $this->input->post('name'),
                'motto' => $this->input->post('motto'),
                'primary_telephone' => $this->input->post('primary_telephone'),
                'province' => $this->input->post('province'),
                'canton' => $this->input->post('canton'),
                'district' => $this->input->post('district'),
                'address_line_1' => $this->input->post('address_line_1'),
                'address_line_2' => $this->input->post('address_line_2'),
                'schedule' => $this->input->post('schedule'),
                'creation_date' => date(DATE_FORMAT),
                'state' => STORE_ACTIVE
            );

            if ($this->form_validation->run()) 
            {
                if ($this->services_model->store_reg($data) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(UNKNOWN_ERROR);
            }
            else 
            {
                $this->_show_form_validation_errors($data);
            }
        } 
        else 
        {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
        
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function store_edit(){
        if ($this->_is_user_logged()) 
        {
            $data = array(
                'id' => $this->input->post('id'),
                'name' => $this->input->post('name'),
                'motto' => $this->input->post('motto'),
                'primary_telephone' => $this->input->post('primary_telephone'),
                'province' => $this->input->post('province'),
                'canton' => $this->input->post('canton'),
                'district' => $this->input->post('district'),
                'address_line_1' => $this->input->post('address_line_1'),
                'address_line_2' => $this->input->post('address_line_2'),
                'schedule' => $this->input->post('district'),
            );

            if ($this->form_validation->run()) 
            {
                if ($this->services_model->store_edit($data) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(UNKNOWN_ERROR);
            }
            else 
            {
                $this->_show_form_validation_errors($data);
            }
        } 
        else 
        {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
        
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function store_del(){
          if ($this->_is_user_logged()) {
            $data = array(
                'id' => $this->input->post('id')
            );

            if ($this->form_validation->run()) {
                if ($this->services_model->store_del($data) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(UNKNOWN_ERROR);
            }
            else {
                $this->_show_form_validation_errors($data);
            }
        } else {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function get_my_stores(){
        if ($this->_is_user_logged()) {
            $data['owner_id'] = $this->session->userdata('user_id');
            $response = $this->services_model->get_my_stores($data);
            if ($response === FALSE)
                $this->_show_error(UNKNOWN_ERROR);
            else
                $this->_show_ok_response($response);
        } else {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
    
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function product_add(){
        if ($this->_is_user_logged()) 
        {
            $data = array(
                'store_id' => $this->input->post('store_id'),
                'name' => $this->input->post('name'),
                'description' => $this->input->post('description'),
                'price' => $this->input->post('price')
            );

            if ($this->form_validation->run()) 
            {
                if ($this->services_model->product_add($data) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(UNKNOWN_ERROR);
            }
            else 
            {
                $this->_show_form_validation_errors($data);
            }
        } 
        else 
        {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
        
    }
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function product_edit(){
        if ($this->_is_user_logged()) 
        {
            $data = array(
                'id' => $this->input->post('id'),
                'name' => $this->input->post('name'),
                'description' => $this->input->post('description'),
                'price' => $this->input->post('price')
            );

            if ($this->form_validation->run()) 
            {
                if ($this->services_model->product_edit($data) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(UNKNOWN_ERROR);
            }
            else 
            {
                $this->_show_form_validation_errors($data);
            }
        } 
        else 
        {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
    
    
    /*
     * author: Lidier
     * purpose: 
     * parameters:
     *
     */
    function product_del(){
        if ($this->_is_user_logged()) {
            $data = array(
                'id' => $this->input->post('id')
            );
            

            if ($this->form_validation->run()) {
                if ($this->services_model->product_del($data) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(UNKNOWN_ERROR);
            }
            else {echo 'aqui';
                $this->_show_form_validation_errors($data);
            }
        } else {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
  
  
  
    //*****************************************************************************
    //*****************************************************************************
    
    
    /*
     * author: Lidier
     * purpose: Get the basic data of a user
     * parameters:
     *
     */
    function user_get() 
    {
        if ($this->_is_user_logged()) 
        {            
            $response = $this->services_model->user_get(array('id' => $this->session->userdata('user_id')));
            if ($response === FALSE)
                $this->_show_error(USER_GET_ERROR);
            else
                $this->_show_ok_response($response);
        } 
        else 
        {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
    /*
     * author: Lidier
     * purpose: Edit the basic data of a user
     * parameters:
     *
     */
    function user_edit() 
    {
        if ($this->_is_user_logged()) 
        {
            $user = array(
            'id' => $this->session->userdata('user_id'),
            'username' => $this->input->post('username'),
            'password' => sha1($this->input->post('password')),
            'password_confirmation' => sha1($this->input->post('password_confirmation')),
            'email' => $this->input->post('email'),
            'firstname' => $this->input->post('firstname'),
            'secondname' => $this->input->post('secondname'),
            'lastname1' => $this->input->post('lastname1'),
            'lastname2' => $this->input->post('lastname2'),
            'id_number' => $this->input->post('id_number'),
        );
            if ($this->form_validation->run()) 
            {
                unset($user['password_confirmation']);
                if ($this->services_model->user_edit($user) === TRUE)
                    $this->_show_ok_response();
                else
                    $this->_show_error(EDIT_ERROR);
            }
            else 
            {
                $this->_show_form_validation_errors($user);
            }
        } 
        else 
        {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
    
   
    /*
     * author: Lidier
     * purpose: Disable a single user (set its state to USER_INACTIVE)
     * parameters:
     *
     */
    function user_disable() {
        if ($this->_is_user_logged()) {
            if ($this->services_model->user_disable(array('id' => $this->session->userdata('user_id'))) === TRUE)
                $this->_show_ok_response();
            else
                $this->_show_error(USER_DISABLE_ERROR);
        } else {
            $this->_show_error(AUTHENTICATION_REQUIRED_ERROR);
        }
    }
    
    /*
     * author: Lidier
     * purpose: Get all the users and their information within a JSON format
     *
     */
    function users_get(){
        $response = $this->services_model->users_get();
        if ($response === FALSE)
            $this->_show_error(USERS_GET_ERROR);
        else
            $this->_show_ok_response($response);
    }
    

    
//*******************************************************************************************************************************
// HIDDEN FUNCTIONS
    
    /*
     * author: Lidier
     * purpose: 
     * parameters: 
     *
     */
    function _is_user_logged()
    {
        if($this->session->userdata('user_id'))
            return true;
        else
            return false;   
        
    }
    
    
    /*
     * author: Lidier
     * purpose: Show a JSON negative response in case of any kind of error
     * parameters: $msg -> the error msg to be displayed
     *
     */
    function _show_error($lang_key){
        echo json_encode(array('status' => ERROR_STATUS, 'message' => lang($lang_key)), JSON_FORCE_OBJECT);
    }
    
    
     /*
     * author: Lidier
     * purpose: Show a JSON negative response in case of form validation errors
     * parameters: $data -> an array containing each one of the form fields and their value
     *
     */
    function _show_form_validation_errors($data){
        $response['status'] = ERROR_STATUS;
        $response['message'] = '';
        foreach ($data as $key => $value){
            $response['message'] = $response['message'].form_error($key).' ';
        }
        echo json_encode($response, JSON_FORCE_OBJECT);
    }
    
    
    /*
     * author: Lidier
     * purpose: Show a JSON positive response (if no errors appear in the process) 
     * parameters:
     *            
     */
    
    function _show_ok_response($data = ''){
        echo json_encode(array('status' => OK_STATUS, 'response' => $data), JSON_FORCE_OBJECT);
    }
    
    
    /*
     * author: Lidier
     * purpose: Saves in the session the information provided 
     * parameters: $data -> could be a single string argument or an array containing several information
     */
    function _create_user_session($data){
        if (is_string($data))
            $this->session->set_userdata('user_id', $data['user_id']);
        elseif (is_array($data)){
            $this->session->set_userdata($data);
        }
    }
    
}

/* End of file services.php */
/* Location: ./application/controllers/services.php */